package com.ruantong.dingtalk.util;

import lombok.Builder;
import lombok.Data;

/**
 * @description:响应基础类
 * @author: jh.chen
 * @time: 2020/11/15 13:30
 */
@Builder
@Data
public class Result<T> {

    public static final String SUCCESS_CODE = "200";
    public static final String SUCCESS_MESSAGE = "Success";
    public static final String FAIL_CODE = "500";
    public static final String FAIL_MESSAGE = "An error occurred on the server!";
    public static final String PARAMETER_ILLEGAL_CODE = "400";
    public static final String PARAMETER_ILLEGAL_MESSAGE = "Parameter is illegal,please check again!";

    private String code;
    private String message;
    private T data;
    private Long totalCount;

    public static Result success() {
        return Result.builder().code(SUCCESS_CODE).message(SUCCESS_MESSAGE).build();
    }

    public static <T> Result success(T data, Long totalCount) {
        return Result.builder().data(data).code(SUCCESS_CODE).message(SUCCESS_MESSAGE)
                .totalCount(totalCount).build();
    }

    public static <T> Result success(T data) {
        return Result.builder().data(data).code(SUCCESS_CODE).message(SUCCESS_MESSAGE).totalCount(1L).build();
    }

    public static Result fail() {
        return fail(null, null);
    }

    public static Result fail(String code, String msg) {
        return Result.builder()
                .code(code == null ? SUCCESS_CODE : code)
                .message(msg == null ? SUCCESS_MESSAGE : msg).build();
    }

    public static Result paramIllegal() {
        return Result.builder().code(PARAMETER_ILLEGAL_CODE).message(PARAMETER_ILLEGAL_MESSAGE).build();
    }
}
